import 'package:dali_ng_pera/global/dnp_global_manager.dart';
import 'package:dali_ng_pera/manager/dnp_cache_manager.dart';
import 'package:dali_ng_pera/plugin/dnp_plugin/lib/dnp_plugin.dart';
import 'package:geolocator/geolocator.dart';

const String kDNPPopTip = "To recommend loan options and related services that meet your local needs, we need to access your location information. Please enable location permission to enjoy a tailored experience.";

class DnpLocationHelper {

  static get needPop async {

    bool needPop = DnpGlobalManager().splashEntity.loudness == 1;
    bool check = await checkPermission;

    if (check == false) return;

    if (needPop && aboveOneDay) {
      DnpPlugin().showAlert(kDNPPopTip);
    }
  }

  static Future<bool> get checkPermission async {
    LocationPermission locationPermission = await Geolocator.checkPermission();
    return locationPermission == LocationPermission.denied ||
        locationPermission == LocationPermission.deniedForever;
  }

  static bool get aboveOneDay {
    DateTime? lastAccessDate = DateTime.tryParse(DnpCacheManager().getString("oneDay") ?? "");
    DateTime time = DateTime.now();

    var t = time.toIso8601String();

    if (lastAccessDate == null) {
      DnpCacheManager().saveString("oneDay", t);
      return true;
    }

    if (time.isAfter(lastAccessDate.add(const Duration(days: 1)))) {
      DnpCacheManager().saveString("oneDay", t);
      return true;
    }

    return false;
  }
}